﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ReportingManagement.Models;
using GenericTools;

namespace ReportingManagement.Components
{
    public partial class PromisTemplateSelector : Form
    {
        private const int TEMPLATE_COLUMN = 0;      // TemplateName
        private const int TEMPLATE_FILE_LOCATION_COLUMN = 1;        // TemplateFileLocation

        private List<ExcelTemplateReport> reportTemplates = null;
        private List<TemplateSelectorViewModel> _SelectedTemplates;

        public PromisTemplateSelector(ExcelTemplateType excelTemplateType, bool showLocation)
        {
            InitializeComponent();
            CreatePromisTemplateDropDownList(excelTemplateType);

            dgvTemplates.Columns[TEMPLATE_FILE_LOCATION_COLUMN].Visible = showLocation;
        }

        public void AddTemplates(List<TemplateSelectorViewModel> templates)
        {
            foreach(var t in templates)
            {
                int index = dgvTemplates.Rows.Add();
                DataGridViewRow row = dgvTemplates.Rows[index];
                row.Cells[TEMPLATE_COLUMN].Value = t.ExcelTemplateId;
                row.Cells[TEMPLATE_FILE_LOCATION_COLUMN].Value = t.TemplateLocation;
            }
        }

        private void CreatePromisTemplateDropDownList(ExcelTemplateType type)
        {
            reportTemplates = new List<ExcelTemplateReport>();
            foreach (var t in PromisTemplateSelector.ExcelTemplateReports)
            {
                if ((t.ExcelTemplateType & type) == type) reportTemplates.Add(t);
            }

            DataGridViewColumn  column = dgvTemplates.Columns["TemplateName"];
            DataGridViewComboBoxColumn templateColumn = (DataGridViewComboBoxColumn)column;

            templateColumn.DisplayMember = "Description";
            templateColumn.ValueMember = "TemplateID";
            templateColumn.DataSource = reportTemplates;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {            
            _SelectedTemplates = new List<TemplateSelectorViewModel>();

            foreach (DataGridViewRow r in dgvTemplates.Rows)
            {                
                DataGridViewCell cell = r.Cells[TEMPLATE_COLUMN];
                var template = new TemplateSelectorViewModel();

                try
                {
                    template.ExcelTemplateId = (int)cell.Value;
                    foreach (var t in reportTemplates)
                    {
                        if (t.TemplateID == template.ExcelTemplateId)
                        {
                            template.DisplayName = t.Description;
                            template.Data = t;
                            break;
                        }
                    }

                    cell = r.Cells[TEMPLATE_FILE_LOCATION_COLUMN];
                    template.TemplateLocation = cell.Value as string;
                    _SelectedTemplates.Add(template);
                }
                catch { }
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        public List<TemplateSelectorViewModel> SelectedTemplates
        {
            get
            {
                return _SelectedTemplates;
            }
        }

        private void dgvTemplates_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == TEMPLATE_FILE_LOCATION_COLUMN)
            {
                DataGridView dgv = (DataGridView)sender;
                openFileDialog.InitialDirectory = Application.StartupPath;
                openFileDialog.FileName = string.Empty;

                DataGridViewRow row = dgv.Rows[e.RowIndex];
                DataGridViewCell cell = row.Cells[TEMPLATE_FILE_LOCATION_COLUMN];
                string currentFile = cell.Value as string;
                if (!string.IsNullOrEmpty(currentFile))
                {
                    if (File.Exists(currentFile))
                    {
                        FileInfo file = new FileInfo(currentFile);
                        openFileDialog.InitialDirectory = file.DirectoryName;
                        openFileDialog.FileName = currentFile;
                    }
                }

                var response = openFileDialog.ShowDialog(this);
                if (response == System.Windows.Forms.DialogResult.OK) cell.Value = openFileDialog.FileName;
            }
        }

        private static List<ExcelTemplateReport> _ExcelTemplateReports;
        public static List<ExcelTemplateReport> ExcelTemplateReports
        {
            get
            {
                return _ExcelTemplateReports;
            }
        }
        
        static PromisTemplateSelector()
        {
            _ExcelTemplateReports = ExcelTemplateReport.GetExcelTemplateReports(ExcelTemplateType.SAPR_APR, false);
        }
    }
}
